//
//  UserNavigationController.m
//  wangfenguser
//
//  Created by wj on 16/4/4.
//  Copyright © 2016年 wj. All rights reserved.
//

#import "UserNavigationController.h"

@interface UserNavigationController ()<UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@end

@implementation UserNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势，调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理，拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
    self.delegate = self;
    
    //设置UINavigationBar的主题
    [self setupNavigationBarTheme];
}

-(void)setupNavigationBarTheme
{
    
    //设置文字属性
    NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
    //设置字体颜色
    textAttrs[NSForegroundColorAttributeName] = CSBlackColor;
    //设置字体
    textAttrs[NSFontAttributeName]= [UIFont boldSystemFontOfSize:18];
    //设置字体的偏移量（0）
    //说明：UIOffsetZero是结构体，只有包装成NSValue对象才能放进字典中
    [self.navigationBar setTitleTextAttributes:textAttrs];
    
    //设置背景颜色
    UIImage* navigationBg = [self GetImageWithColor:CSWhiteColor andHeight:CZQ_StatusBarHeight + CZQ_NavigationBarHeight];
    
    //设置背景颜色
    [self.navigationBar setBackgroundImage:navigationBg forBarMetrics:UIBarMetricsDefault];
    
    if (self.isLoginNav) {
     
        self.navigationBar.shadowImage = [UIImage new];
    }
    else{
        
        UIImage *shadowImage = [self GetImageWithColor:BackGroundColor andHeight:1];
        
        self.navigationBar.shadowImage = shadowImage;
    }
    self.navigationBar.translucent = NO;
}

#pragma mark 重写父类的方法
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        viewController.navigationItem.leftBarButtonItem = leftButtonItem;
    }
    
    [super pushViewController:viewController animated:YES];
    
    // 修改tabBra的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}


/**
 * 设置状态栏背景颜色
 **/
-(UIStatusBarStyle)preferredStatusBarStyle{

    return UIStatusBarStyleDefault;
}

#pragma mark UIGestureRecognizerDelegate

// 什么时候调用：每次触发手势之前都会询问下代理，是否触发。
// 作用：拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    // 注意：只有非根控制器才有滑动返回功能，根控制器没有。
    // 判断导航控制器是否只有一个子控制器，如果只有一个子控制器，肯定是根控制器
    if (self.childViewControllers.count == 1) {
        // 表示用户在根控制器界面，就不需要触发滑动手势，
        return NO;
    }
    return YES;
}
#pragma mark UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
    // Enable the gesture again once the new controller is shown
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
        self.interactivePopGestureRecognizer.enabled = YES;
}

#pragma mark UINavationControllerDelegate
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]] || [viewController isKindOfClass:[ArangViewController class]]|| [viewController isKindOfClass:[EngageController class]] || [viewController isKindOfClass:[MyViewController class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
    
}


/**
 *  返回方法
 *
 */
-(void)back
{

    [self popViewControllerAnimated:YES];
}


-(void)setIsLoginNav:(BOOL)isLoginNav{
    
    _isLoginNav = isLoginNav;
    
    if (isLoginNav) {
        
        self.navigationBar.shadowImage = [UIImage new];
    }
    else{
        
        UIImage *shadowImage = [self GetImageWithColor:BackGroundColor andHeight:1];
        
        self.navigationBar.shadowImage = shadowImage;
    }
}
/**
 *  生成图片
 *
 *  @param color  图片颜色
 *  @param height 图片高度
 *
 *  @return 生成的图片
 */
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

@end
